﻿@using SimpleIdServer.IdServer.Api.RegistrationWorkflows;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.AuthMethodsStore;

@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IState<AuthMethodsState> authMethodsState
@inject IDispatcher dispatcher

@if (editForm != null && !authMethodsState.Value.IsLoading)
{
    <RadzenTemplateForm Data="@editForm" TItem="RegistrationWorkflowEditForm" Submit="@Submit">
        <RadzenStack>
            <!-- Name -->
            <RadzenFormField Text="@Global.Name" Variant=Variant.Outlined>
                <ChildContent>
                    @if (IsEditionMode)
                    {
                        <RadzenTextBox Name="Name" @bind-Value="@editForm.Name" Disabled=true></RadzenTextBox>
                    }
                    else
                    {
                        <RadzenTextBox Name="Name" @bind-Value="@editForm.Name"></RadzenTextBox>
                    }
                </ChildContent>
                <Helper>
                    <RadzenRequiredValidator Component="Name" Text="@Global.NameIsRequired" />
                </Helper>
            </RadzenFormField>
            <!-- Is Default -->
            <div class="mt-3">
                <RadzenCheckBox Name="IsDefault" @bind-Value=editForm.IsDefault></RadzenCheckBox>
                <RadzenLabel Text="@Global.IsDefault" Component="IsDefault" Style="margin-left: 8px; vertical-align: middle;" />
            </div>
        </RadzenStack>
        <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(IsUpdating ? (IsEditionMode ? Global.Updating : Global.Adding) : (IsEditionMode ? Global.Update : Global.Add))" Disabled=@IsUpdating />
    </RadzenTemplateForm>
}

@code {
    [Parameter]
    public RegistrationWorkflowResult RegistrationWorkflow { get; set; }
    [Parameter]
    public bool IsEditionMode { get; set; }
    [Parameter]
    public bool IsUpdating { get; set; }
    [Parameter]
    public EventCallback<RegistrationWorkflowResult> Saved { get; set; }
    RegistrationWorkflowEditForm editForm = new RegistrationWorkflowEditForm();
    List<string> AuthMethods
    {
        get 
        {
            return authMethodsState.Value.AuthenticationMethods.Where(a => a.Capabilities.HasFlag(AuthenticationMethodCapabilities.USERREGISTRATION)).Select(a => a.Id).ToList();
        }
    }

    class RegistrationWorkflowEditForm
    {
        public string Name { get; set; }
        public bool IsDefault { get; set; }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if(IsEditionMode)
        {            
            if(RegistrationWorkflow != null)
            {
                editForm = new RegistrationWorkflowEditForm
                {
                    IsDefault = RegistrationWorkflow.IsDefault
                };
            }
        }
        else
        {
            editForm = new RegistrationWorkflowEditForm();
        }
    }

    async void Submit(RegistrationWorkflowEditForm ef)
    {
        var result = new RegistrationWorkflowResult { Id = RegistrationWorkflow?.Id, Name = ef.Name, IsDefault = ef.IsDefault };
        await Saved.InvokeAsync(result);
    }
}